﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;

namespace FOBTV.Web.DBUtility
{
	/// <summary>
	/// 分页模型类
	/// </summary>
	public class PagingEntity
	{
		private string _ASCString;
		private string _DESCString;
		private int _PageIndex;
		private bool _Paging;
		private int _PageSize;
		private int _RecordCount;
		private string _SelectString;
		private string _TableName;
		private string _WhereClause;

		public PagingEntity()
		{
		}
		public PagingEntity(string TableName, string[] SelectStrings, string Order, int PageSize, int PageIndex)
		{
			this._ASCString = Order;
			this._DESCString = GetDeOrder();
			this._SelectString = "*";
			if (SelectStrings != null)
			{
				StringBuilder sbCol = new StringBuilder();
				int length = SelectStrings.Length;
				if (length > 0)
				{
					foreach (string ColumnName in SelectStrings)
					{
						sbCol.Append(ColumnName + ",");
					}
					this._SelectString = sbCol.ToString(0, sbCol.Length - 1);
				}
			}
			this._PageSize = PageSize;
			this._PageIndex = PageIndex;
			this._TableName = TableName;
			this._Paging = true;
		}

		public PagingEntity(string TableName, string Order, int PageSize, int PageIndex)
		{
			this._ASCString = Order;
			this._DESCString = GetDeOrder();
			this._SelectString = "*";
			this._PageSize = PageSize;
			this._PageIndex = PageIndex;
			this._TableName = TableName;
			this._Paging = true;
		}
		public PagingEntity(string TableName, string Order)
		{
			this._ASCString = Order;
			this._DESCString = GetDeOrder();
			this._SelectString = "*";
			this._PageSize = 1;
			this._PageIndex = 1;
			this._TableName = TableName;
			this._Paging = true;
		}

		private string GetDeOrder()
		{
			if (this._ASCString.Length <= 0)
			{
				return string.Empty;
			}
			string[] orders = this._ASCString.Split(new char[] { ',' });
			StringBuilder sb = new StringBuilder();
			foreach (string order in orders)
			{
				string[] o = order.Trim().Split(new char[] { ' ' });
				string deorder = o[0];
				if (o.Length <= 1)
				{
					deorder = deorder + " desc";
				}
				sb.Append(deorder);
				sb.Append(",");
			}
			return sb.ToString(0, sb.Length - 1);
		}

		public string AscString
		{
			get
			{
				return this._ASCString;
			}
			set
			{
				this._ASCString = value;
			}
		}

		public string DescString
		{
			get
			{
				return this._DESCString;
			}
			set
			{
				this._DESCString = value;
			}
		}

		public int PageIndex
		{
			get
			{
				if (this.Paging)
				{
					return this._PageIndex;
				}
				return 1;
			}
			set
			{
				this._PageIndex = value;
			}
		}

		public int PageSize
		{
			get
			{
				if (this.Paging)
				{
					return this._PageSize;
				}
				return this.RecordCount;
			}
			set
			{
				this._PageSize = value;
			}
		}

		public bool Paging
		{
			get
			{
				return this._Paging;
			}
			set
			{
				this._Paging = value;
			}
		}

		public int RecordCount
		{
			get
			{
				return this._RecordCount;
			}
			set
			{
				this._RecordCount = value;
			}
		}

		public string SelectString
		{
			get
			{
				return this._SelectString;
			}
			set
			{
				this._SelectString = value;
			}
		}

		public string TableName
		{
			get
			{
				return this._TableName;
			}
			set
			{
				this._TableName = value;
			}
		}

		public string WhereClause
		{
			get
			{
				return this._WhereClause;
			}
			set
			{
				this._WhereClause = value;
			}
		}
	}
}